<?php
/**
 * 创建榜单-热度榜
 * 更新时间：每周一，3点42
 */
ini_set('display_errors', 1);
ini_set('memory_limit', '512M');
error_reporting(-1);
require_once '/data/wwwroot/g_vendor/autoload.php';

use Biz\model\bigData\BrandRankingHot as bigBrandRankingHot;
use Biz\model\brand\Brand as mBrand;
use Biz\model\wiki\WikiRankingConfig;
use Biz\model\wiki\WikiRankingHot;
use Biz\model\wiki\WikiRankingWeek;
use Biz\service\wikiRanking\Ranking;
use Biz\service\wikiRanking\AdmRanking;

//付费品牌可在某一个榜单中（例如：沙发热度榜 or 沙发口碑榜）出现 2 个商品，非付费品牌仅可在一个榜单中出现 1 个商品；
$start = time();
$dayArr = Ranking::getCreateRankingDay();
$day = $dayArr['day'];
$sourceDay = $dayArr['source_day'];
$now = date('Ymd');
if ($sourceDay != $now) {
    var_dump('当前日期为：' . $now . '不能创建：' . $sourceDay . '的榜单');
    return false;
}
$info = WikiRankingWeek::getByWhere(['ranking_day' => $day]);
if (!$info) {
    var_dump('不存在的榜单期数');
    return false;
}
$allRankingList = WikiRankingConfig::selectByWhere(['config_state[!]' => WikiRankingConfig::CONFIG_STATE_DEL, 'pid[>]' => 0, 'deep' => 2], ['id', 'ranking_name']);
$allRankingList = array_column($allRankingList, null, 'id');

foreach ($allRankingList as $rankingId => $configInfo) {
    $rankingName = $configInfo['ranking_name'];
    $hotWhere = [
        'day' => $sourceDay,
        'ranking_id' => $rankingId,
        'LIMIT' => [0, 100],
        'ORDER' => ['score' => 'DESC']
    ];
    $bigHot = bigBrandRankingHot::select('*', $hotWhere);

    if (!empty($bigHot)) {
        $brandIds = [];
        $bigBrandHotList = [];
        $insertData = [];
        //获取picId
        $basic_pic_list = AdmRanking::getBasicPicIdsByRankingList($bigHot);
        //获取picId
        foreach ($bigHot as $bigRow) {
            if ($bigRow['brand_id']) {
                $bigBrandHotList[$bigRow['brand_id']][] = $bigRow;
                $brandIds[] = $bigRow['brand_id'];
            } else {
                $insertData[] = [
                    'ranking_day' => $day,
                    'ranking_id' => $rankingId,
                    'ranking_name' => $rankingName,
                    'basic_id' => $bigRow['basic_id'],
                    'brand_id' => $bigRow['brand_id'],
                    'basic_pic_id' => $basic_pic_list[$bigRow['basic_id']] ?: '',
                    'score' => $bigRow['score'],
                    'addtime' => date('Y-m-d H:i:s'),
                    'edittime' => date('Y-m-d H:i:s'),
                ];
            }
        }

        $brandIds = array_unique($brandIds);
        $brandList = [];
        if ($brandIds) {
            $brandList = mBrand::select(['id', 'brand_type'], ['id' => $brandIds]);
            $brandList = array_column($brandList, null, 'id');
        }
        foreach ($bigBrandHotList as $brandId => $brandRows) {
            $brandInfo = isset($brandList[$brandId]) ? $brandList[$brandId] : [];
            //一个品牌同一个榜单下只能有1个商品
            var_dump('一个品牌同一个榜单下只能有1个商品：' . $brandId . ":" . count($brandRows));
            $rows = array_slice($brandRows, 0, 1);
            foreach ($rows as $row) {
                $insertData[] = [
                    'ranking_day' => $day,
                    'ranking_id' => $rankingId,
                    'ranking_name' => $rankingName,
                    'basic_id' => $row['basic_id'],
                    'basic_pic_id' => $basic_pic_list[$row['basic_id']] ?: '',
                    'brand_id' => $row['brand_id'],
                    'score' => $row['score'],
                    'addtime' => date('Y-m-d H:i:s'),
                    'edittime' => date('Y-m-d H:i:s'),
                ];
            }
        }
    }

    if ($insertData) {
        $soreSort = array_column($insertData, 'score');
        array_multisort($soreSort, SORT_DESC, $insertData);
        $insertData = array_slice($insertData, 0, 100);
        var_dump('创建「' . $rankingName . '」' . '#' . $rankingId . '的热度榜单');
        WikiRankingHot::insert($insertData);
    }
}

//检查是否创建数据成功
$check = WikiRankingHot::selectCountWhere(['ranking_day' => $day]);
if (!$check) {
    throw new \Exception('未查询到创建的' . $day . '热度榜榜单数据');
}

$end = time();
var_dump(date('Y-m-d-H-i-s') . '第' . $day . '期榜单,创建榜单-热度榜,耗时:' . ($end - $start) . '秒，' . (($end - $start) / 60) . '分');
//10秒内
